//
//  R4Controller.m
//  TheRicher
//
//  Created by 李晓东 on 2018/2/5.
//  Copyright © 2018年 PeanutXu. All rights reserved.
//

#import "R12Controller.h"
#import "R12Cell.h"

@interface R12Controller () <DatabaseResultDelegate>

@end

@implementation R12Controller

- (void)loadView{
    [super loadView];
    
    self.dataArray = [NSMutableArray array];
    self.netService = [NetService share];
    self.databaseService = [DatabaseService share];
    
    [self.tableView setDataSource:self];
    [self.tableView registerClass:[R12Cell class] forCellReuseIdentifier:@"TableCellId"];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // 1.先查询数据库
    self.databaseService.delegate = self;
    self.tableName = ShuangSeQiuTableName;
    [self.databaseService queryTable:ShuangSeQiuTableName withCondition:nil ofConditionName:nil andJudgeFlag:nil andOrderBy:@"issueno" andSortBy:1 andCountOf:RowCount];
    
}




- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    R12Cell *cell = [tableView dequeueReusableCellWithIdentifier:@"TableCellId" forIndexPath:indexPath];
    [cell setValues:self.dataArray[indexPath.row]];
    [cell setSelectedBackgroundView:[[UIView alloc]initWithFrame:cell.bounds ]];
    [cell.selectedBackgroundView setBackgroundColor:[UIColor redColor]];
    
    return cell;
}

- (void)resultInDatabase:(NSArray *)resultArray{
    [[KillNumberTools share] type12:resultArray];
    if ([self.dataArray count] > 0) {
        if ([[(ShuangSeQiu *)self.dataArray.lastObject number] containsString:[(ShuangSeQiu *)resultArray[0] killingNumber]]) {
            [(ShuangSeQiu *)self.dataArray.lastObject setDeadthNumber:[resultArray[0] killingNumber]];
        }
    }
    for (int i = 0; i < resultArray.count - 1; i++ ) {
        if ([[(ShuangSeQiu *)resultArray[i] number] containsString:[(ShuangSeQiu *)resultArray[i + 1] killingNumber]] ) {
            [(ShuangSeQiu *)resultArray[i] setDeadthNumber:[(ShuangSeQiu *)resultArray[i + 1] killingNumber]];
        }
    }
    
    [self.dataArray addObjectsFromArray:resultArray];
    [self.tableView reloadData];
    
    if ([self.activityIndicatorView isAnimating]) {
        [self.activityIndicatorView stopAnimating];
    }
}

- (void)typeMethodInKillNumberTools:(NSArray *)resultArray{
    [[KillNumberTools share]type12:resultArray];
}


- (void)judgeKillingNumber:(NSInteger)resultCount{
    for (int i = 0; i < resultCount + 1; i++) {
        if( [(ShuangSeQiu *)self.dataArray[i] judgeWhetherItContain:[(ShuangSeQiu *)self.dataArray[i+1] killingNumber
                                                                     ]]) {
            [(ShuangSeQiu *)self.dataArray[i] setDeadthNumber:[(ShuangSeQiu *)self.dataArray[i + 1] killingNumber]];
        }
    }
    self.needToRefreshCount = 0;
    [self.tableView reloadData];
}

- (void)refreshChildControllerView:(NSInteger)refreshCount{
    [self judgeKillingNumber:refreshCount];
}

- (void)resultNeedToInsertKillingDatabase:(NSArray *)insertArray{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [[DatabaseService share] insertTableOfKillingNumberWithObjects:insertArray inColumn:@"type12"];
        
    });
}
@end

